/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: jian yi, eyjian@qq.com
 */
#ifndef MOOON_SERVER_WAITER_POOL_H
#define MOOON_SERVER_WAITER_POOL_H
#include <stdexcept>
#include <mooon/utils/array_queue.h>
#include "waiter.h"
#include "mooon/server/server.h"
SERVER_NAMESPACE_BEGIN

class CWorkThread;
class CWaiterPool
{
public:
    ~CWaiterPool();
    CWaiterPool(CWorkThread* thread, IFactory* factory, uint32_t waiter_count) throw(std::runtime_error);
    
    void destroy();
    void create(uint32_t connection_count, IFactory* factory);
    bool is_valid() const { return _waiter_queue->capacity() > 0; }

    CWaiter* pop_waiter();
    void push_waiter(CWaiter* connection);

private:
    void init_waiter(CWaiter* connection) throw(std::runtime_error);
    
private:    
    CWorkThread* _thread;
    CWaiter* _waiter_array;
    IFactory* _factory;
    utils::CArrayQueue<CWaiter*>* _waiter_queue;
};

SERVER_NAMESPACE_END
#endif // MOOON_SERVER_WAITER_POOL_H
